<%@Page Language="VB"%>

<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %>

<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Specifying the Columns in a DataGrid Control</title>
<style type="text/css">
body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
input {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}

.rHead {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial;
        font-size:14pt; font-weight:bold; padding:8px; color:green}
.rItem {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial,sans-serif;
        font-size:10pt}
.rFoot {font-family:Tahoma,Arial; font-size:8pt; padding:8px; color:darkgray}

</style></head>
<body bgcolor="#ffffff">
<span class="heading">Specifying the Columns in a DataGrid Control</span><hr />
<!--------------------------------------------------------------------------->

<%'-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server" />

<div id="outError" runat="server" />

<form runat="server">

  'Release Date' Column:
  <ASP:RadioButton id="chkVisible" GroupName="Col2Visible" runat="server"
                   AutoPostback="True" /> Visible &nbsp;
  <ASP:RadioButton id="chkNotVisible" GroupName="Col2Visible" runat="server"
                   AutoPostback="True" /> Hidden<p />

  <ASP:DataGrid id="MyDataGrid" runat="server"
       AutoGenerateColumns="False"
       CellPadding="5"
       GridLines="None"
       HeaderStyle-BackColor="silver"
       HeaderStyle-HorizontalAlign="center"
       FooterStyle-BackColor="silver"
       ShowFooter="True"
       OnItemCommand="ShowInfo">
    <Columns>
      <ASP:TemplateColumn HeaderText="" ItemStyle-BackColor="silver">
        <ItemTemplate>&nbsp;</ItemTemplate>
      </ASP:TemplateColumn>
      <ASP:BoundColumn HeaderText="<b>Code</b>" DataField="ISBN" ItemStyle-BackColor="lightblue" />
      <ASP:BoundColumn HeaderText="<b>Book Title</b>" DataField="Title"/>
      <ASP:BoundColumn HeaderText="<b>Released</b>" DataField="PublicationDate" DataFormatString="{0:D}" ItemStyle-HorizontalAlign="right" ItemStyle-BackColor="yellow" />
      <ASP:TemplateColumn HeaderText="" ItemStyle-BackColor="lightblue">
        <ItemTemplate>
          <ASP:Button id="cmdInfo" Text="More Info" CommandName="Info" runat="server" />
        </ItemTemplate>
      </ASP:TemplateColumn>
      <ASP:TemplateColumn HeaderText="<b>Buy Now</b>" ItemStyle-BackColor="silver" ItemStyle-HorizontalAlign="center">
        <ItemTemplate>
          <ASP:CheckBox id="chkBuy" runat="server" />
        </ItemTemplate>
      </ASP:TemplateColumn>
    </Columns>
  </ASP:DataGrid><p />

  <ASP:Label id="lblInfo" runat="server" /><br />

</form>

<!--------------------------------------------------------------------------->

<script language="vb" runat="server">


Sub Page_Load()

   If Page.IsPostback Then

      'display or hide the "Release Date" column
      MyDataGrid.Columns(3).Visible = (chkVisible.Checked = True)

   Else

      chkVisible.Checked = True  'set default value
      BindDataGrid()             'create data set and bind grid

   End If

End Sub


Sub ShowInfo(objSender As Object, objArgs As DataGridCommandEventArgs)

  'runs when any command button in the grid is clicked
  'see if the CommandName of the clicked button was "Info"
  If objArgs.CommandSource.CommandName = "Info" Then

    'get values of ISBN and Title from Text property of the table cells
    'for the current row returned in the objArgs parameter values
    Dim strISBN As String = objArgs.Item.Cells(1).Text
    Dim strTitle As String = objArgs.Item.Cells(2).Text

    'display the informaion in the page - possibly extract from database?
    lblInfo.Text = "More information about the book:<br /><b>" & strTitle _
                 & "</b><br />(ISBN " &strISBN & ") goes here..."
  End If

End Sub


Sub BindDataGrid()

   'get connection string from ..\global\connect-strings.ascx user control
   Dim strConnect As String  = ctlConnectStrings.OLEDBConnectionString

   'create a SQL statement to select some rows from the database
   Dim strSelect As String
   strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '18610023%'"

   'create a variable to hold an instance of a DataReader object
   Dim objDataReader As OleDbDataReader

   Try

      'create a new Connection object using the connection string
      Dim objConnect As New OleDbConnection(strConnect)

      'open the connection to the database
      objConnect.Open()

      'create a new Command using the connection object and select statement
      Dim objCommand As New OleDbCommand(strSelect, objConnect)

      'execute the SQL statement against the command to get the DataReader
      objDataReader = objCommand.ExecuteReader()

    Catch objError As Exception

       'display error details
       outError.InnerHtml = "<b>* Error while accessing data</b>.<br />" _
           & objError.Message & "<br />" & objError.Source & "<p />"
       Exit Sub  ' and stop execution

    End Try

   'set the DataSource property of the DataList
   MyDataGrid.DataSource = objDataReader

   'and bind the control to the data
   MyDataGrid.DataBind()

End Sub

</script>

<!--------------------------------------------------------------------------->
<hr /><span class="cite">[<a href="../global/viewsource.aspx">view source</a>] &nbsp; &nbsp; &nbsp; &nbsp;
&copy;2001 <a class="cite" href="http://www.wrox.com/">Wrox Press</a> -
<a class="cite" href="http://www.wrox.com/Books/Book_Details.asp?isbn=1861004885">Professional ASP.NET</a> (ISBN: 1-861004-88-5)</span>
</body>
</html>
